{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "ad1c7490",
   "metadata": {},
   "source": [
    "- 2022/3/24\n",
    "- week05\n",
    "- 本周主要内容：图像识别（人脸识别）"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bc261911",
   "metadata": {},
   "source": [
    "# 上周内容："
   ]
  },
  {
   "cell_type": "markdown",
   "id": "740fe352",
   "metadata": {},
   "source": [
    "### 1. 人脸识别概念和基础\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eeded321",
   "metadata": {},
   "source": [
    "### 2. 人脸识别应用\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8cc8bdd4",
   "metadata": {},
   "source": [
    "\n",
    "### 3. 人脸识别实践-face++"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e1221668",
   "metadata": {},
   "source": [
    "> 1. API文档阅读规范\n",
    "> 2. 人脸检测（人脸分析）实践\n",
    "> 3. 输入/输出\n",
    "> 4. 错误码寻找原因"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2bb83b94",
   "metadata": {},
   "source": [
    "#### 百度ai开放平台"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fe353d20",
   "metadata": {},
   "source": [
    "- 阿里ai开放平台 电商"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "78aa193e",
   "metadata": {},
   "source": [
    "- 腾讯ai"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ef338dae",
   "metadata": {},
   "source": [
    "- face++ 旷视 人脸识别"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f50d7f1b",
   "metadata": {},
   "source": [
    "- 讯飞开放平台 语音 人脸识别"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d2f5f664",
   "metadata": {},
   "source": [
    "# 人脸识别"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "45f13871",
   "metadata": {},
   "source": [
    "# face++ 旷视"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "77fb4fe8",
   "metadata": {},
   "source": [
    "#### 基于人脸的人流量统计，教室签到"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "37a18c5f",
   "metadata": {},
   "source": [
    "# 人脸识别概念和基础"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "c42edd3f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'request_id': '1679662182,581ab4fc-4d3d-4281-a99f-8b38afa18cc6', 'time_used': 102, 'faces': [{'face_token': '40b504e371cfffdc87109750329ef751', 'face_rectangle': {'top': 99, 'left': 185, 'width': 110, 'height': 110}}], 'image_id': '2TpPSLI2H40fpa5gZkW0Gg==', 'face_num': 1}\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "Face++ 人脸识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://api-cn.faceplusplus.com/facepp/v3/detect\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('xuezhiqian.jpg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\n",
    "    \"api_key\":\"PYPld-DPAmor-b1WI3PnRMpvX9Xqt3HD\",\n",
    "    \"api_secret\":\"lZc_4Qdi8JcAqELCZaZaCGaoWjd1Ww6u\",\n",
    "    \"image_base64\":img,\n",
    "}\n",
    "\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "95182414",
   "metadata": {},
   "source": [
    "### 解释： params参数信息"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e4e3d299",
   "metadata": {},
   "source": [
    "# 人脸对比"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "1d67aab1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'request_id': '1680790702,248bc2ca-7151-46f5-bce3-9c7e3403b61b', 'time_used': 319, 'confidence': 77.221, 'thresholds': {'1e-3': 62.327, '1e-4': 69.101, '1e-5': 73.975}, 'faces1': [{'face_token': 'b452579a2d7ed17b7e0502ea1081795d', 'face_rectangle': {'top': 203, 'left': 381, 'width': 288, 'height': 288}}], 'faces2': [{'face_token': 'e44a3cfeb0c0f01f40e1d41ea184078e', 'face_rectangle': {'top': 97, 'left': 166, 'width': 97, 'height': 97}}], 'image_id1': 'NncAA3IDay0CgBT3BSffrA==', 'image_id2': 'A9mgi1NnzYqMBUSFk4fYPA=='}\n",
      "200\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "人脸比对\n",
    "'''\n",
    "\n",
    "request_url = \"https://api-cn.faceplusplus.com/facepp/v3/compare\"\n",
    "# 二进制方式打开图片文件\n",
    "f_01 = open('xuezhiqian_01.jpg', 'rb')\n",
    "f_02 = open('xuezhiqian_02.jpg', 'rb')\n",
    "img_01 = base64.b64encode(f_01.read())\n",
    "img_02 = base64.b64encode(f_02.read())\n",
    "\n",
    "params = {\n",
    "    \"api_key\":\"PYPld-DPAmor-b1WI3PnRMpvX9Xqt3HD\",\n",
    "    \"api_secret\":\"lZc_4Qdi8JcAqELCZaZaCGaoWjd1Ww6u\",\n",
    "    \"image_base64_1\":img_01,\n",
    "    \"image_base64_2\":img_02,\n",
    "}\n",
    "\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "print (response.json())\n",
    "print (response.status_code)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "81c15831",
   "metadata": {},
   "source": [
    "# 2.人脸搜索"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "37687459",
   "metadata": {},
   "source": [
    "## 创建人脸数据集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "a967196b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "200\n",
      "{'faceset_token': '749b0403206260161022e0f4f7c8366c', 'time_used': 179, 'face_count': 0, 'face_added': 0, 'request_id': '1680791190,9903a654-3011-4abf-9091-4c6eef07f42b', 'outer_id': '', 'failure_detail': []}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "import requests\n",
    "'''\n",
    "人脸数据库创建\n",
    "'''\n",
    "request_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/create\"\n",
    "# 请求参数\n",
    "params = {\n",
    "    \"api_key\":\"PYPld-DPAmor-b1WI3PnRMpvX9Xqt3HD\",\n",
    "    \"api_secret\":\"lZc_4Qdi8JcAqELCZaZaCGaoWjd1Ww6u\",\n",
    "    \"display_name\":\"广州南方学院网新1班\"\n",
    "}\n",
    "response = requests.post(request_url, data=params)\n",
    "\n",
    "print (response.status_code)\n",
    "print (response.json())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "97db9073",
   "metadata": {},
   "source": [
    "### ps：params参数信息"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f893abc7",
   "metadata": {},
   "source": [
    "## 检查人脸数据库的情况"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "99168ab9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "200\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '689f8e1cf460b1346a420f32deb2af15',\n",
       " 'tags': '',\n",
       " 'time_used': 101,\n",
       " 'user_data': '',\n",
       " 'display_name': '广州南方学院1栋宿舍楼',\n",
       " 'face_tokens': ['7d2ef00e802300d8c6538511b4f2c684',\n",
       "  '675479a01a9a5779ae71ea8db08b189e',\n",
       "  '9d3219e623e96472d78a94de417ad77f'],\n",
       " 'face_count': 3,\n",
       " 'request_id': '1680846500,de76a2ca-b94a-4ff1-aef8-6219d1fa3235',\n",
       " 'outer_id': ''}"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "import requests\n",
    "'''\n",
    "人脸数据集合详情查询\n",
    "'''\n",
    "request_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/getdetail\"\n",
    "# 请求参数\n",
    "params = {\n",
    "    \"api_key\":\"jRpCaZ34kqNYJ8Zppdc-yGGum_YkETov\",\n",
    "    \"api_secret\":\"Vr0PtRCw-ZFwYXTKVfi6aDNaSqlfunK3\",\n",
    "    \"faceset_token\":\"689f8e1cf460b1346a420f32deb2af15\"\n",
    "\n",
    "}\n",
    "response = requests.post(request_url, data=params)\n",
    "\n",
    "print (response.status_code)\n",
    "response.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2f2fa138",
   "metadata": {},
   "source": [
    "## 增加人脸信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "7da1fc36",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'request_id': '1680793830,fade7902-834b-41e2-a84e-61db1113d8db', 'time_used': 112, 'faces': [{'face_token': 'd1e941c7420f62498f9b888c700ff088', 'face_rectangle': {'top': 153, 'left': 105, 'width': 294, 'height': 294}}], 'image_id': 'y6fk7DVTPEYfXAydXp72gw==', 'face_num': 1}\n",
      "200\n"
     ]
    }
   ],
   "source": [
    "# 先检测人脸信息\n",
    "\n",
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "人脸检测: 王鹤棣、虞书欣、迪丽热巴\n",
    "'''\n",
    "\n",
    "request_url = \"https://api-cn.faceplusplus.com/facepp/v3/detect\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('1班/whd.jpg', 'rb')\n",
    "\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "\n",
    "params = {\n",
    "    \"api_key\":\"PYPld-DPAmor-b1WI3PnRMpvX9Xqt3HD\",\n",
    "    \"api_secret\":\"lZc_4Qdi8JcAqELCZaZaCGaoWjd1Ww6u\",\n",
    "    \"image_base64\":img,\n",
    "\n",
    "}\n",
    "\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "print (response.json())\n",
    "print (response.status_code)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "91c2421a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "200\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '689f8e1cf460b1346a420f32deb2af15',\n",
       " 'time_used': 104,\n",
       " 'face_count': 3,\n",
       " 'face_added': 0,\n",
       " 'request_id': '1680846548,589fd019-b79e-4e87-b333-63678770b435',\n",
       " 'outer_id': '',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "import requests\n",
    "'''\n",
    "增加人脸\n",
    "'''\n",
    "request_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/addface\"\n",
    "# 请求参数\n",
    "params = {\n",
    "    \"api_key\":\"jRpCaZ34kqNYJ8Zppdc-yGGum_YkETov\",\n",
    "    \"api_secret\":\"Vr0PtRCw-ZFwYXTKVfi6aDNaSqlfunK3\",\n",
    "    \"faceset_token\":\"689f8e1cf460b1346a420f32deb2af15\",\n",
    "    \"face_tokens\":'9d3219e623e96472d78a94de417ad77f'\n",
    "\n",
    "}\n",
    "response = requests.post(request_url, data=params)\n",
    "\n",
    "print (response.status_code)\n",
    "response.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2a28adb9",
   "metadata": {},
   "source": [
    "## 检查人脸集合的人脸信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5807b12b",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a5202671",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7395ca2f",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2cd2d300",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3efe0206",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1f1b535f",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c6a29e0b",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c88fe68f",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f36523a1",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "22af8d56",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d8302bd1",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "adf91cdc",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a59585e3",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
